﻿using UnityEngine;
using UnityEditor;
using UnityEditor.Graphs;

public class StateGUIData<S>  where S : StateDataBase
{
    public S stateData;

    public Rect GUIRect
    {
        get
        {
            Rect r = new Rect();
            r.center = stateData.editorPosition ;
            r.size = SM_EditorConstValue.State_GUI_Size;
            return r;
        }
    }

    public MachineStateUIStateEnum uiState = MachineStateUIStateEnum.Normal;
    public Styles.Color styleColor
    {
        get
        {
            Styles.Color c = StateMachineUtils.GetStyleColorByState(uiState);
            if (uiState == MachineStateUIStateEnum.Normal)
                c = NormalStateColor;
            return c;
        }
    }

    private Styles.Color normalStateColor;
    public Styles.Color NormalStateColor
    {
        get
        {
            return normalStateColor;
        }
        set
        {
            if (value == Styles.Color.Blue || value == Styles.Color.Green)
                normalStateColor = Styles.Color.Gray;
            else
                normalStateColor = value;
        }
    }

    public void DrawGUI()
    {
        bool isOn = uiState == MachineStateUIStateEnum.Normal ? false : true;
        GUIStyle style = Styles.GetNodeStyle("node", styleColor, isOn);
        style.fontSize = SM_EditorConstValue.FontSize; 
        GUI.Label(GUIRect, stateData. name, style);
        style.fontSize = 0;
        stateData.editorPosition = DragLimit(GUIRect);

    }
    private  Vector2 DragLimit(Rect rect)
    {
      float extraLenth=  SM_EditorConstValue.Bg_GUI_ExtraLenth;
        float x = rect.xMin - extraLenth * 2;
        float y = rect.yMin - extraLenth * 2;
        x = Mathf.Clamp(x, SM_DrawBGGUIUtils.bgRect.xMin, SM_DrawBGGUIUtils.bgRect.xMax);
        y = Mathf.Clamp(y, SM_DrawBGGUIUtils.bgRect.yMin, SM_DrawBGGUIUtils.bgRect.yMax);
        x += extraLenth * 2;
        y += extraLenth * 2;
        return new Vector2(x, y);
    }

}

public enum MachineStateUIStateEnum
{
    Normal,
    select,
    Running,
}